﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.Linq.SqlClient;

public partial class ContractInformation : System.Web.UI.UserControl
{
    BankDataDataContext db = new BankDataDataContext();

    protected decimal getMonthAmount()
    {
        int contractId = getContractId();
        Contract selectedContract = getContract(contractId);
        if (selectedContract != null)
        {
            decimal amount = (selectedContract.Amount - getPayedMainAmount()) / (selectedContract.TermLength - getMonthCountPayment());
            return amount;
        }
        return -1;
    }

    protected decimal getPayedMainAmount()
    {
        var payments = from payment in db.ContractPayments where payment.ConractId == getContractId() select payment;
        decimal payedAmount = 0;
        foreach (ContractPayment contractPayment in payments)
        {
            payedAmount += contractPayment.MainAmount;
        }
        return payedAmount;
    }

    protected decimal getPayedPercentageAmount()
    {
        var payments = from payment in db.ContractPayments where payment.ConractId == getContractId() select payment;
        decimal payedAmount = 0;
        foreach (ContractPayment contractPayment in payments)
        {
            payedAmount += contractPayment.PercentageAmount;
        }
        return payedAmount;
    }

    protected double getMonthCreditPercentage()
    {
        Contract contract = getContract(getContractId());
        var creditCurrencies = from creditCurrency in db.CreditCurrencies
                               where creditCurrency.CreditId == contract.CreditId &&
                                   creditCurrency.CurrencyId == contract.CurrencyId
                               select creditCurrency;
        return creditCurrencies.First().Percentage / 12;
    }

    protected int getMonthCountFromLastPayment()
    {
        var payments = from payment in db.ContractPayments where payment.ConractId == getContractId() select payment;
        Contract contract = getContract(getContractId());
        DateTime maxDate = contract.CreationDate;
        foreach (ContractPayment contractPayment in payments)
        {
            if (contractPayment.Date > maxDate)
            {
                maxDate = contractPayment.Date;
            }
        }
        return SqlMethods.DateDiffMonth(maxDate, DateTime.Now);
    }

    protected int getMonthCountPayment()
    {
        var payments = from payment in db.ContractPayments where payment.ConractId == getContractId() select payment;
        Contract contract = getContract(getContractId());
        DateTime maxDate = contract.CreationDate;
        foreach (ContractPayment contractPayment in payments)
        {
            if (contractPayment.Date > maxDate)
            {
                maxDate = contractPayment.Date;
            }
        }
        return SqlMethods.DateDiffMonth(contract.CreationDate, maxDate);
    }

    protected Contract getContract(int contractId)
    {
        var contracts = from contract in db.Contracts where contract.ContractId == contractId select contract;
        if (contracts.Count() > 0)
        {
            return contracts.First();
        }
        return null;
    }

    protected int getContractId()
    {
        string contractId = null;
        if (Session["contractId"] != null)
        {
            contractId = Session["contractId"].ToString();
            return Convert.ToInt32(contractId);
        }
        return -1;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Contract selectedContract = getContract(getContractId());
        if (selectedContract != null)
        {
            decimal creditAmount = (decimal)selectedContract.Amount;
            AllAmount.Text = creditAmount.ToString();
            PayedAmount.Text = getPayedMainAmount().ToString();
            DebtAmount.Text = (creditAmount - getPayedMainAmount()).ToString();
            PercentageAmount.Text = getPayedPercentageAmount().ToString();
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}